package com.cdshop.web;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.jsp.PageContext;

import com.cdshop.entity.User;
import com.cdshop.web.auth.AuthenticateService;
import com.cdshop.web.auth.Identity;

public final class PageUtil {

	protected PageUtil() {
	}

	public static String string(String value) {
		return value == null ? "" : value;
	}
	
	public static String toString(String value) {
		return value == null ? "" : value;
	}
	
	public static int toInt(Long val) {
		return val == null ? -1 : val.intValue();
	}

	public static JSONObj json() {
		return JSONObj.instance();
	}

	public static String concat(Object... params) {
		StringBuilder sb = new StringBuilder();
		for (Object p : params) {
			sb.append(p);
		}
		return sb.toString();
	}

	protected static ThreadLocal<SimpleDateFormat> dateFormat = new ThreadLocal<SimpleDateFormat>() {
		@Override
		protected SimpleDateFormat initialValue() {
			return new SimpleDateFormat();
		}
	};

	public static String formatDate(Date date, String pattern) {
		if (date == null)
			return null;
		SimpleDateFormat format = dateFormat.get();
		format.applyPattern(pattern);
		return format.format(date);
	}

	public static Date parseDate(String val, String pattern) {
		if (val == null)
			return null;
		SimpleDateFormat format = dateFormat.get();
		format.applyPattern(pattern);
		try {
			return format.parse(val);
		} catch (ParseException e) {
			return null;
		}
	}

	protected static ThreadLocal<DecimalFormat> numberFormat = new ThreadLocal<DecimalFormat>() {

		@Override
		protected DecimalFormat initialValue() {
			return new DecimalFormat();
		}

	};

	public static String formatNumber(double num, String pattern) {

		DecimalFormat format = numberFormat.get();
		format.applyPattern(pattern);
		return format.format(num);
	}

	public static String formatNumber(long num, String pattern) {
		DecimalFormat format = numberFormat.get();
		format.applyPattern(pattern);
		return format.format(num);
	}

	public static String formatNumber(Number num, String pattern) {
		if (num == null)
			return null;
		DecimalFormat format = numberFormat.get();
		format.applyPattern(pattern);
		return format.format(num);
	}

	public static Number parseNumber(String val, String pattern) {
		if (val == null)
			return null;
		DecimalFormat format = numberFormat.get();
		format.applyPattern(pattern);
		try {
			return format.parse(val);
		} catch (ParseException e) {
			return null;
		}
	}

	public static final boolean equals(Object obj1, Object obj2) {
		return (obj1 == obj2) || (obj1 != null && obj1.equals(obj2));
	}

	public static final String JSON_DATE_PATTERN = "MM/dd/yyyy";
	public static final String JSON_NUMBER_PATTERN = "0";

	public static boolean isLoggedIn(PageContext pctx) {
		return AuthenticateService.instance().isLoggedIn(pctx.getSession());
	}

	public static User loginUser(PageContext pctx) {
		Identity identity = AuthenticateService.instance().getIdentity(
				pctx.getSession());
		return identity == null ? null : identity.getUser();
	}
}
